package com.swgk.core.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.StringRes;
import androidx.databinding.DataBindingUtil;

import com.swgk.core.R;
import com.swgk.core.databinding.DialogRoundInputBinding;


/**
 * 圆角输入弹窗Dialog
 */
public class RoundInputDialog extends Dialog {

    private Context context;
    private ConfirmCallback confirmCallback;
    private TextView tvErrorMessage;
    private TextView tvConfirm;
    private TextView tvCancel;
    private EditText etInput;

    public RoundInputDialog(Context context) {
        super(context, R.style.RoundCornerDialog);
        this.context = context;
        initDialog();
    }

    /**
     * 初始化Dialog
     */
    public void initDialog() {
        Window win = getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        win.setGravity(Gravity.RELATIVE_LAYOUT_DIRECTION | Gravity.CENTER);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_round_input);
        initView();
        initListener();
    }

    private void initView() {
        tvErrorMessage = findViewById(R.id.tv_error_message);
        tvConfirm = findViewById(R.id.tv_confirm);
        tvCancel = findViewById(R.id.tv_cancel);
        etInput = findViewById(R.id.et_input);
    }

    private void initListener() {
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmCallback != null) {
                    confirmCallback.onClick(v, etInput.getText().toString().trim());
                }
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setErrorMessage(String info) {
        tvErrorMessage.setText(info);
    }

    public void setConfirmCallback(ConfirmCallback confirmCallback) {
        this.confirmCallback = confirmCallback;
    }

    public interface ConfirmCallback {
        /**
         * 点击确定后得接口
         *
         * @param v
         * @param intPutString
         */
        void onClick(View v, String intPutString);
    }
}
